import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_unionad/flutter_unionad.dart';
import 'package:hxdj/common/env_config/env.dart';
import 'package:hxdj/common/my_common.dart';
import 'package:hxdj/common/my_get_it.dart';
import 'package:hxdj/common/my_global.dart';
import 'package:hxdj/common/routers/page_routers_manager.dart';
import 'package:hxdj/message/rongcloud/rc_engine_manager.dart';
import 'package:hxdj/splash.dart';
import 'package:provider/provider.dart';

import 'common/routers/base_routers.dart';
import 'common/routers/login_routers_manager.dart';

startApp() async {
  //是 Flutter 框架中的一行代码，主要用于确保 Flutter 的核心库和绑定被正确地初始化。
  WidgetsFlutterBinding.ensureInitialized();

  // 强制竖屏
  SystemChrome.setPreferredOrientations(
      <DeviceOrientation>[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);

  // 初始化路由
  LoginRouterManager().initRouter(Routes.router);
  PageRouterManager().initRouter(Routes.router);

  //GetIt
  setupServiceLocator();

  //本地缓存
  await CommonPlugin.getSharedPreferences();
  //获取当前APP的版本
  await CommonPlugin.getAppVersion();

  //全局loading
  configLoading();

  //适配
  await ScreenUtil.ensureScreenSize();

  //高的地图
  AMapFlutterLocation.updatePrivacyShow(true, true);
  AMapFlutterLocation.updatePrivacyAgree(true);
  AMapFlutterLocation.setApiKey("anroid ApiKey", Env.getInstance()!.gaoDeIoSAppKey);

  //穿山甲
  bool isSuccess = await FlutterUnionad.register(
      //穿山甲广告 Android appid 必填
      androidAppId: Env.getInstance()!.csjAndroidAppId,
      //穿山甲广告 ios appid 必填
      iosAppId: Env.getInstance()!.csjIosAppId,
      //使用TextureView控件播放视频,默认为SurfaceView,当有SurfaceView冲突的场景，可以使用TextureView 选填
      useTextureView: true,
      //appname 必填
      appName: CommonPlugin.packageInfo!.appName,
      //是否允许sdk展示通知栏提示 选填
      allowShowNotify: true,
      //是否在锁屏场景支持展示广告落地页 选填
      allowShowPageWhenScreenLock: true,
      //是否显示debug日志
      debug: true,
      //是否支持多进程，true支持 选填
      supportMultiProcess: true,
      //是否开启个人性推荐 选填
      personalise: FlutterUnionadPersonalise.open,

      //允许直接下载的网络状态集合 选填
      directDownloadNetworkType: [
        FlutterUnionadNetCode.NETWORK_STATE_2G,
        FlutterUnionadNetCode.NETWORK_STATE_3G,
        FlutterUnionadNetCode.NETWORK_STATE_4G,
        FlutterUnionadNetCode.NETWORK_STATE_WIFI
      ]);
  debugPrint("注册穿山甲广告, 是否成功 === $isSuccess");
  //初始化融云
  await RCEngineManager().initRongClound();
  // 启动应用
  runApp(ChangeNotifierProvider<MyGlobalViewModel>(
    create: (context) => MyGlobalViewModel(),
    child: const MyApp(),
  ));
  await Future.delayed(const Duration(seconds: 2));
  FlutterNativeSplash.remove();
}

void configLoading() {
  EasyLoading.instance
    ..displayDuration = const Duration(milliseconds: 2000)
    ..indicatorType = EasyLoadingIndicatorType.fadingCircle
    ..loadingStyle = EasyLoadingStyle.dark
    ..indicatorSize = 45.0
    ..radius = 10.0
    ..textColor = Colors.yellow
    ..maskColor = Colors.blue.withOpacity(0.5)
    ..userInteractions = true
    ..dismissOnTap = false;
}
